<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option label="待审核" value="待审核" />
          <el-option label="已通过" value="已通过" />
          <el-option label="已拒绝" value="已拒绝" />
          <el-option label="修改待审核" value="修改待审核" />
        </el-select>
      </el-form-item>
      <el-form-item label="题目状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择题目状态" clearable>
          <el-option label="开放" value="开放" />
          <el-option label="已满" value="已满" />
          <el-option label="已关闭" value="已关闭" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="topicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题目ID" align="center" prop="topicId" width="80" />
      <el-table-column label="题目名称" align="center" prop="title" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="难度等级" align="center" prop="difficulty" width="100">
        <template slot-scope="scope">
          <el-tag :type="getDifficultyType(scope.row.difficulty)" size="small">{{ scope.row.difficulty }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="名额" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.currentStudents || 0 }} / {{ scope.row.maxStudents }}</span>
        </template>
      </el-table-column>
      <el-table-column label="题目状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)" size="small">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" width="130">
        <template slot-scope="scope">
          <div>
            <el-tag :type="getAuditStatusType(scope.row.auditStatus)" size="small">
              {{ scope.row.auditStatus }}
            </el-tag>
            <!-- 修改待审核状态显示特殊图标 -->
            <el-tooltip v-if="scope.row.auditStatus === '修改待审核'" content="您的修改正在审核中，学生端暂时看到的是原内容" placement="top">
              <i class="el-icon-warning-outline" style="color: #E6A23C; margin-left: 5px; cursor: pointer;"></i>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题目对话框 -->
    <topic-form ref="topicForm" @success="getList" />

    <!-- 查看题目详情对话框 -->
    <el-dialog title="题目详情" :visible.sync="detailVisible" width="900px" append-to-body>
      <el-descriptions :column="2" border v-if="detailData">
        <el-descriptions-item label="题目ID">{{ detailData.topicId }}</el-descriptions-item>
        <el-descriptions-item label="难度等级">
          <el-tag :type="getDifficultyType(detailData.difficulty)" size="small">{{ detailData.difficulty }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="题目名称" :span="2">{{ detailData.title }}</el-descriptions-item>
        <el-descriptions-item label="最大学生数">{{ detailData.maxStudents }}</el-descriptions-item>
        <el-descriptions-item label="当前学生数">{{ detailData.currentStudents || 0 }}</el-descriptions-item>
        <el-descriptions-item label="题目状态">
          <el-tag :type="getStatusType(detailData.status)" size="small">{{ detailData.status }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <el-tag :type="getAuditStatusType(detailData.auditStatus)" size="small">{{ detailData.auditStatus }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="技术标签" :span="2">
          <el-tag 
            v-for="tag in getTagsArray(detailData.tags)" 
            :key="tag" 
            size="small" 
            style="margin-right: 8px;"
          >{{ tag }}</el-tag>
          <span v-if="!detailData.tags || getTagsArray(detailData.tags).length === 0">暂无</span>
        </el-descriptions-item>
        <el-descriptions-item label="题目描述" :span="2">
          <div style="white-space: pre-wrap;">{{ detailData.description }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="技能要求" :span="2">
          <div style="white-space: pre-wrap;">{{ detailData.requirements || '暂无' }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailData.remark || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间" :span="2">{{ parseTime(detailData.createTime) }}</el-descriptions-item>
        
        <!-- 如果有待审核的修改，显示对比信息 -->
        <template v-if="detailData.hasPendingChanges === 1">
          <el-descriptions-item :span="2">
            <div slot="label" style="color: #E6A23C; font-weight: bold;">
              <i class="el-icon-warning"></i> 待审核的修改内容
            </div>
          </el-descriptions-item>
          <el-descriptions-item label="待审核题目名称" :span="2" v-if="detailData.pendingTitle">
            <span style="color: #409EFF; font-weight: bold;">{{ detailData.pendingTitle }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="待审核难度等级" :span="2" v-if="detailData.pendingDifficulty">
            <el-tag :type="getDifficultyType(detailData.pendingDifficulty)" size="small">{{ detailData.pendingDifficulty }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="待审核最大学生数" :span="2" v-if="detailData.pendingMaxStudents">
            <span style="color: #409EFF; font-weight: bold;">{{ detailData.pendingMaxStudents }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="待审核技术标签" :span="2" v-if="detailData.pendingTags">
            <el-tag 
              v-for="tag in getTagsArray(detailData.pendingTags)" 
              :key="tag" 
              size="small" 
              type="warning"
              style="margin-right: 8px;"
            >{{ tag }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="待审核题目描述" :span="2" v-if="detailData.pendingDescription">
            <div style="white-space: pre-wrap; color: #409EFF;">{{ detailData.pendingDescription }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="待审核技能要求" :span="2" v-if="detailData.pendingRequirements">
            <div style="white-space: pre-wrap; color: #409EFF;">{{ detailData.pendingRequirements }}</div>
          </el-descriptions-item>
        </template>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacherTopic, delTopic, exportTopic } from "@/api/system/topic";
import TopicForm from "./TopicForm";

export default {
  name: "TeacherTopic",
  components: {
    TopicForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题目表格数据
      topicList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        auditStatus: null,
        status: null
      },
      // 查看详情相关
      detailVisible: false,
      detailData: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询题目列表 */
    getList() {
      this.loading = true;
      listTeacherTopic(this.queryParams).then(response => {
        this.topicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.topicId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.topicForm.openDialog();
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.detailData = row;
      this.detailVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const topicId = row.topicId || this.ids[0];
      this.$refs.topicForm.openDialog(topicId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const topicIds = row.topicId ? [row.topicId] : this.ids;
      this.$modal.confirm('是否确认删除选中的题目？').then(() => {
        return delTopic(topicIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/topic/export', {
        ...this.queryParams
      }, `topic_${new Date().getTime()}.xlsx`)
    },
    /** 解析标签JSON字符串为数组 */
    getTagsArray(tagsStr) {
      if (!tagsStr) return [];
      try {
        return JSON.parse(tagsStr);
      } catch (e) {
        return [];
      }
    },
    /** 难度等级标签类型 */
    getDifficultyType(difficulty) {
      const map = {
        '简单': 'success',
        '中等': '',
        '困难': 'danger'
      };
      return map[difficulty] || 'info';
    },
    /** 题目状态标签类型 */
    getStatusType(status) {
      const map = {
        '开放': 'success',
        '已满': 'warning',
        '已关闭': 'info'
      };
      return map[status] || 'info';
    },
    /** 审核状态标签类型 */
    getAuditStatusType(auditStatus) {
      const map = {
        '待审核': 'warning',
        '已通过': 'success',
        '已拒绝': 'danger',
        '修改待审核': 'warning'
      };
      return map[auditStatus] || 'info';
    }
  }
};
</script>